<template>
	<view class="summary">
		<scroll-view scroll-y :class="notEdit ? '' : 'scroll-height'">
			<view class="topContent">
				<view class="title">{{topTile}}</view>
				<view class="time">
					{{$t('inspection.taskExecute.actualEndTime')}}
					<text class="blue">{{resultData.consumeTime}}</text>
					{{$t('baseCommon.time.hours')}}
				</view>
			</view>
			<view class="bottomCon">
				<view class="flexBox ">
					<view class="one">
						<view class="num blue">{{resultData.areaCount}}</view>
						<view>{{$t('inspection.stat.totalArea')}}</view>
					</view>
					<view class="one noBorder" @click="toInfo">
						<view class="num orange">{{resultData.areaNoCheckCount}}</view>
						<view>{{$t('inspection.stat.uncheckedArea')}}</view>
					</view>
				</view>
				<view class="flexBox">
					<view class="one">
						<view class="num blue">{{resultData.planCheckCount}}</view>
						<view>{{$t('inspection.stat.totalInspection')}}</view>
					</view>
					<view class="one" @click="toInfo">
						<view class="num orange">{{resultData.noCheckCount}}</view>
						<view>{{$t('inspection.stat.missedInspection')}}</view>
					</view>
					<view class="one noBorder">
						<view class="num blue">{{resultData.exemptCheckCount}}</view>
						<view>{{$t('inspection.stat.exemptionInspection')}}</view>
					</view>
				</view>
				<view class="flexBox">
					<view class="one">
						<view class="num blue">{{resultData.normalCount}}</view>
						<view>{{$t('inspection.stat.normalInspection')}}</view>
					</view>
					<view class="one" @click="toInfo">
						<view class="num orange">{{resultData.exceptionCount}}</view>
						<view>{{$t('inspection.stat.exceptionInspection')}}</view>
					</view>
					<view class="one" @click="toInfo">
						<view class="num orange">{{resultData.abnormalCount}}</view>
						<view>{{$t('inspection.stat.abnormalInspection')}}</view>
					</view>
					<view class="one noBorder">
						<view class="num blue">{{resultData.defectCount}}</view>
						<view>{{$t('inspection.stat.defectCount')}}</view>
					</view>
				</view>
			</view>
			<view class="summaryInput">
				<textarea class="usertextarea" v-model="finishReport" auto-height :disabled="notEdit"
					:placeholder="$t('baseCommon.message.pleaseEnter', { title: $t('inspection.taskPlan.finishReport') })" />
			</view>

			<view v-if="!notEdit" class="summaryFile">
				<attach-file :attachFile="attachFile" :canEdit="true" businessCode="insp"
					subBusinessCode="task" :businessDataCode="$t('inspection.taskPlan.finishReport')" @fileSet="fileSet">
				</attach-file>
			</view>

			<view v-else class="summaryFile">
				<attach-file :attachFile="attachFile"></attach-file>
			</view>

			<view class="applyContent">
				<label class="applyText" @click="applyCheck">
					<checkbox class="applyBox" :checked="isCheked" :disabled="notEdit || resultData.noCheckCount == 0" />
					{{ $t('inspection.taskExecute.isEfi') }}
				</label>
			</view>
			<view class="reasonInput">
				<textarea class="usertextarea" v-model="efiReason" auto-height :disabled="notEdit || resultData.noCheckCount == 0"
					:placeholder="$t('baseCommon.message.pleaseEnter', { title: $t('inspection.taskExecute.efiReason') })" />
			</view>
		</scroll-view>
		
		<view class="submitBtn" v-if="!notEdit">
			<button type="primary" @click="userSubmit">{{$t('baseCommon.buttons.submit')}}</button>
		</view>
	</view>
</template>

<script>
	import { getTaskSummaryStat, saveFinishReport, getTaskInfo, getExemptionInsp } from '@/api/business/inspect.js'
	import { notice, utilities } from '@/utils/common.js'

	export default {
		data() {
			return {
				taskId: '',
				finishReport: '',
				topTile: '',
				notEdit: false,
				resultData: {},
				attachFile: '',
				isCheked: false,
				isEfi: 0,
				efiReason: ''
			}
		},
		onLoad(option) {
			this.taskId = option.taskId;
			this.topTile = option.topTile;
			if (option.notEdit === 'true') {
				this.notEdit = true
			}
			this.$nextTick(() => {
				this.loadData()
			})
		},
		methods: {
			async loadData() {
				let param = {
					'taskId': this.taskId,
				}
				uni.showLoading({
					title: this.$t('common.loading')
				})
				await getTaskSummaryStat(param).then((res) => {
					if (!res) {
						return
					}
					uni.hideLoading()
					if (res.data) {
						this.resultData = res.data;
					}
				})
				await getTaskInfo(this.taskId).then(res => {
					if (!res) {
						return
					}
					uni.hideLoading()
					if (res.data) {
						this.finishReport = res.data.finishReport
						this.attachFile = res.data.attachFile
					}
				})
				await getExemptionInsp({ taskId: this.taskId }).then(res => {
					if(res.data) {
						this.isEfi = res.data.isEfi
						this.isCheked = !!this.isEfi
						this.efiReason = res.data.efiReason
					}
				})
			},
			userSubmit() {
				if(this.isEfi == 1 && !this.efiReason) {
					notice.toast(this.$t('baseCommon.message.pleaseEnter', { title: this.$t('inspection.taskExecute.efiReason') }))
					return
				}
				
				let param = {
					"finishReport": this.finishReport,
					"attachFile": this.attachFile,
					"taskId": this.taskId,
					"isEfi": this.isEfi,
					"efiReason": this.efiReason
				}
				saveFinishReport(param).then((res) => {
					if (!res) {
						return
					}
					if (res.code) {
						notice.toast(this.$t('baseCommon.message.submitSuccess'))
					}
					
					// utilities.backMore(1, 2)
					uni.redirectTo({
						url: '/pages/business/inspect/inspectList'
					})
				})

				// 关闭传感器连接
				uni.closeBluetoothAdapter({
					success(res) {
						console.log('关闭蓝牙模块:' + res);
						uni.removeStorageSync("bindSensor")
					}
				})
			},
			applyCheck() {
				if(this.notEdit || this.resultData.noCheckCount == 0) {
					return
				}
				this.isCheked = !this.isCheked
				this.isEfi = this.isCheked ? 1 : 0
			},
			toInfo() {
				uni.navigateBack()
			},
			fileSet(data) {
				this.attachFile = data
			}
		}
	}
</script>

<style lang="scss" scoped>
	.summary {
		background: #F1F1F1;
	}

	.scroll-height {
		/* #ifdef APP-PLUS */
		height: calc(100vh - 65px);
		/* #endif*/
		/* #ifdef H5 */
		height: calc(100vh - 110px);
		/* #endif*/
	}

	.topContent {
		background: white;
		font-size: 16px;

		.title {
			padding: 12px 10px;
			color: #333;
		}
		
		.time {
			color: #666;
			margin: 0 10px;
			margin-bottom: 8px;
			padding-bottom: 12px;
		
			.blue {
				margin: 0 5px;
				font-size: 18px;
			}
		}
	}

	.bottomCon {
		background: white;
		margin-bottom: 8px;
	}

	.flexBox {
		display: flex;
		align-items: center;
		padding: 5px 0;
		border-bottom: 1rpx solid #eee;
	}

	.one {
		text-align: center;
		font-size: 12px;
		color: #999;
		padding-bottom: 5px;
		margin-top: 10px;
		border-right: 1px solid #ddd;
		width: 50%;
		margin-bottom: 4px;

		.num {
			font-size: 30px;
			font-weight: 500;
		}
	}

	.noBorder {
		border-right: none;
	}

	.blue {
		color: $uni-color-primary;
	}

	.orange {
		color: $uni-color-warning;
	}

	.summaryInput {
		padding: 12px;
		font-size: 12px;
		background: white;

		.usertextarea {
			width: calc(100% - 12px);
			min-height: 150px;
			border: 1px solid #eee;
			padding: 10px 5px;
		}
	}

	.summaryFile {
		margin-bottom: 10px;
	}

	.applyContent {
		background-color: #fff;
	}

	.applyText {
		display: flex;
		align-items: center;
		padding: 10px 12px;
		font-size: 14px;
	}

	.applyBox {
		transform: scale(0.7);
	}

	.reasonInput {
		padding: 0 12px;
		padding-bottom: 10px;
		font-size: 12px;
		background: white;

		.usertextarea {
			width: calc(100% - 12px);
			min-height: 100px;
			border: 1px solid #eee;
			padding: 10px 5px;
		}
	}
	
	.submitBtn {
		position: fixed;
		bottom: 10px;
		width: 96%;
		left: 2%;
		margin: 0 auto;
	
		button {
			background-color: #1E91E6;
		}
	}
	
	::v-deep .fileinfo {
		padding: 0 12px;
	}
	
	::v-deep .n-file-content {
		padding-bottom: 10px;
	}
	
	::v-deep .no-data {
		margin-bottom: 0;
	}
</style>
